import unittest
import requests

from api.login import LoginAPI


class TestLogin(unittest.TestCase):

    def setUp(self):
        self.login_api = LoginAPI()
        self.session = requests.Session()

    def test01_login_success(self):
        response1 = self.login_api.get_verify_code(self.session)
        self.assertEqual(200, response1.status_code)
        self.assertIn("image", response1.headers.get("Content-Type"))

        response2 = self.login_api.login(self.session, '18177542589', '123456', '8888')
        print(response2.json())
        self.assertEqual(200, response2.status_code)
        self.assertEqual(1, response2.json().get("status"))
        self.assertIn("登陆成功", response2.json().get("msg"))

    def test02_user_isnot_exist(self):
        response1 = self.login_api.get_verify_code(self.session)
        self.assertEqual(200, response1.status_code)
        self.assertIn("image", response1.headers.get("Content-Type"))

        response2 = self.login_api.login(self.session, '18177542999', '123456', '8888')
        print(response2.json())
        self.assertEqual(200, response2.status_code)
        self.assertEqual(-1, response2.json().get("status"))
        self.assertIn("账号不存在", response2.json().get("msg"))

    def test03_password_error(self):
        response1 = self.login_api.get_verify_code(self.session)
        self.assertEqual(200, response1.status_code)
        self.assertIn("image", response1.headers.get("Content-Type"))

        response2 = self.login_api.login(self.session, '18177542589', '123666', '8888')
        print(response2.json())
        self.assertEqual(200, response2.status_code)
        self.assertEqual(-2, response2.json().get("status"))
        self.assertIn("密码错误", response2.json().get("msg"))

    def tearDown(self):
        if self.session:
            self.session.close()











